const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 1,
    model2:1,
    hide:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onUnload: function () {
    this.setData({
      hide: 0
    })

  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var money =e.detail.value.money;
    console.log(money);
    var decribe = e.detail.value.decribe.length > 0 ? e.detail.value.decribe:"";
    
    if (money <= 0 || money == null) {
      wx.showModal({
        title: '',
        content: '输入金额必须为正数',
      })
      return;
    }
    if (e.detail.value.pwd.length == 0 ) {
      wx.showModal({
        title: '',
        content: '支付密码不能为空',
      })
      return;
    }
    money = parseFloat(money);
    var apinameIsMeb = "/Action/ZKPay/StartPayment";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        MebID: (wx.getStorageSync('userinfo')).MebID,
        Type: "P",
        Money: money,
        Detail: decribe + '',
        PayPsw: md.hexMD5(e.detail.value.pwd),
        AccID: wx.getStorageSync('depid')
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0 && res.data.data.length > 0) {
          that.setData({
            model: 0,
            model2: 1,
            payno: res.data.data,
            // detail: detail,
            // money: money,
          })
          that.getmq();
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
          })
          return;
        }
      }
    })
    return;
    that.setData({
      money:money,
      detail:decribe,
      model:0,
      model2:0
    })
    

  },
  cancer: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  makesurepwd:function(){
    var that =this;
    
  },
  getpwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  getmq: function () {
    var that = this;
    if (that.data.hide != 1) {
      return;
    }
    var datas = {};
    datas.Key = "zkwallet/payno/" + that.data.payno;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data) {
        that.playaudio();
        wx.redirectTo({
          url: '/pages/success/success?name=' + res.data.data.MebName + "&photo=" + res.data.data.PhotoUrl + "&money=" + res.data.data.Money + "&paytype=支付" + "&state=" + res.data.data._BOState,
        })
      } else {
        that.getmq();
      }
    }
    var res2 = post.getdata("/System/GetMessages", datas, datacall);
  },
  playaudio: function () {

    console.log(1);
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = 'http://www.zokoo.com.cn/api/pos/189.mp3'
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })

  }

})